﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Practices.ServiceLocation;
using Pap.Core.Utilities;
using Pap.Data;
using Pap.Data.Abstraction.Utils;
using Pap.Services;
using Pap.Services.WorkflowEngine;
using Pap.Web.Utils;
using Utils;

namespace Pap.Web.Modules.Loan.ViewModel
{
    /// <summary>
    /// 
    /// </summary>
    public class CancelLoanDebtsViewModel : IViewModel
    {

        /// <summary>
        /// 
        /// </summary>
        ILoanRepository _LoanRepository;
        /// <summary>
        /// 
        /// </summary>
        IWorkflowService _WfService;
        /// <summary>
        /// 
        /// </summary>
        ILoanExecutionService _LoanExecutionService;

        /// <summary>
        /// Initializes a new instance of the <see cref="CancelLoanDebtsViewModel"/> class.
        /// </summary>
        /// <param name="loanRepository">The loan repository.</param>
        /// <param name="workflowService">The workflow service.</param>
        /// <param name="loanExecutionService">The loan execution service.</param>
        public CancelLoanDebtsViewModel(ILoanRepository loanRepository, IWorkflowService workflowService, ILoanExecutionService loanExecutionService)
        {
            _LoanRepository = loanRepository;
            _WfService = workflowService;
            _LoanExecutionService = loanExecutionService;
        }


        /// <summary>
        /// Gets the loan.
        /// </summary>
        /// <param name="loanID">The loan ID.</param>
        /// <returns></returns>
        public ICustomerLoan GetLoan(Guid loanID)
        {
            return _LoanRepository.GetCustomerLoanByID(loanID);
        }

        /// <summary>
        /// Cancels the debts.
        /// </summary>
        /// <param name="loanID">The loan ID.</param>
        /// <param name="ReasonOfDebtsCanceling">The reason of debts canceling.</param>
        /// <param name="insertedAttachment">The inserted attachment.</param>
        /// <param name="deletedIDs">The deleted I ds.</param>
        /// <param name="reasons">The reasons.</param>
        public void CancelDebts(Guid loanID, string ReasonOfDebtsCanceling, IEnumerable<Guid> insertedAttachment, IEnumerable<Guid> deletedIDs, string reasons)
        {
            using (IUnitOfWork trans = ServiceLocator.Current.GetInstance<IUnitOfWork>())
            {
                ILoanCancellingDebt loanCancelDebt = ServiceLocator.Current.GetInstance<ILoanCancellingDebt>();
                loanCancelDebt.LoanCancellingDebtID = Guid.NewGuid();
                loanCancelDebt.LoanID = loanID;
                loanCancelDebt.Notes = reasons;
                _LoanRepository.CreateLoanCancellingDebt(loanCancelDebt);

                foreach (var attachID in insertedAttachment)
                {
                    ILoanCancellingDebtAttachment loanCancelAttach = ServiceLocator.Current.GetInstance<ILoanCancellingDebtAttachment>();
                    loanCancelAttach.LoanCancellingDebtAttachmentID = Guid.NewGuid();
                    loanCancelAttach.LoanCancellingDebtID = loanCancelDebt.LoanCancellingDebtID;
                    loanCancelAttach.Attachment = attachID;
                    _LoanRepository.CreateLoanCancellingDebtAttachment(loanCancelAttach);
                }

                _LoanRepository.DeleteLoanCancellingDebtAttachments(att => deletedIDs.Contains(att.Attachment));

                var processInst = _WfService.GetProcessInstanceIDByEntity(loanID);
                _WfService.Move(ActionTypes.CancelDebts, processInst, RegistryInfo.UserID, reasons, null);


                var lastLoanExecution = _LoanRepository.GetCustomerLoanByID(loanID).LoanExecutionLst.Where(ex => ex.LoanExecutionStatus).OrderBy(ex => ex.ExecutionDate).Last();


                LoanHistoryCloseTypes loanClosType;
                var lastActionType = _WfService.GetStep(processInst).ActionType;
                if (Enums.ToString(lastActionType).Equals(Enums.ToString(ActionTypes.Stumble)))
                    loanClosType = LoanHistoryCloseTypes.CanelDebts;
                else
                    loanClosType = LoanHistoryCloseTypes.Prescution;

                var loanHistory = _LoanExecutionService.GetLoanHistory(lastLoanExecution, loanClosType, string.Empty);
                _LoanRepository.CreateCustomerLoanHistory(loanHistory);

                trans.Commit();
            }
        }
    }
}
